package damngoodcoffee.smart;

import hotsax.html.sax.SaxParser;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.XMLReader;

import com.google.inject.AbstractModule;
import com.google.inject.Provider;
import com.google.inject.Provides;
import com.thoughtworks.selenium.CommandProcessor;
import com.thoughtworks.selenium.HttpCommandProcessor;

import damngoodcoffee.smart.commands.SeleniumCommand;
import damngoodcoffee.smart.interpreters.AnchorInterpreter;
import damngoodcoffee.smart.interpreters.CodeInterpreter;
import damngoodcoffee.smart.interpreters.Interpreter;

public class SmartModule extends AbstractModule {
    @Override
    protected void configure() {
	bind(Parser.class).to(ParserImpl.class);
	bind(SmartContentHandler.class).to(SmartContentHandlerImpl.class);
	bind(XMLReader.class).to(SaxParser.class);
	bind(ResourceReader.class).to(ResourceReaderImpl.class);
	bind(CommandProcessor.class).to(HttpCommandProcessor.class);
	// bind(CommandProcessor.class).to(NotSoSmartCommandProcessor.class);
	bind(Test.class).to(TestImpl.class);
    }

    @Provides
    List<Interpreter> provideInterpreters(Provider<Parser> parser,
	    Provider<SeleniumCommand> provider) {
	List<Interpreter> interpreters = new ArrayList<Interpreter>();
	interpreters.add(new AnchorInterpreter(parser));
	interpreters.add(new CodeInterpreter(provider));
	return interpreters;
    }

    @Provides
    NotSoSmartCommandProcessor provideNotSoSmartCommandProcessor() {
	return new NotSoSmartCommandProcessor();
    }

    @Provides
    HttpCommandProcessor provideCommandProcessor() {

	HttpCommandProcessor l = null;
	if (threadLocalHttpCommandProcessor.get() != null) {
	    l = threadLocalHttpCommandProcessor.get();
	} else {
	    try {
		l = new HttpCommandProcessor("localhost", 4444, "*firefox",
			"http://www.google.com");
		threadLocalHttpCommandProcessor.set(l);
	    } catch (Exception e) {
		e.printStackTrace();
	    }
	}

	return l;
    }

    private static ThreadLocal<HttpCommandProcessor> threadLocalHttpCommandProcessor = new ThreadLocal<HttpCommandProcessor>();

}
